package com.dl.exam.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.dl.exam.entity.Page;
import com.dl.exam.entity.question.SingleQuestion;
import com.dl.exam.entity.question.SubjectiveQuestion;
import com.dl.exam.mapper.SubjectiveMapper;
import com.dl.exam.service.SubjectiveService;
import com.dl.exam.util.PageUtils;
import com.dl.exam.util.UUIDUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-01-25 16:57
 */
@Service
@Transactional
public class SubjectiveServiceImpl implements SubjectiveService {

    @Resource
    private SubjectiveMapper subjectiveMapper;

    @Override
    public boolean addQuestion(SubjectiveQuestion subjectiveQuestion) {
        subjectiveQuestion.setQuestionId(UUIDUtils.getUUID10());
        return subjectiveMapper.insertQuestion(subjectiveQuestion) != 0;
    }

    @Override
    public boolean removeQuestion(String questionId) {
        return subjectiveMapper.deleteQuestion(questionId) != 0;
    }

    @Override
    public boolean modifyQuestion(JSONObject questionForm) {
        return subjectiveMapper.updateQuestion(questionForm) != 0;
    }

    @Override
    public SubjectiveQuestion getQuestionById(String questionId) {
        return subjectiveMapper.queryQuestionById(questionId);
    }

    @Override
    public List<SingleQuestion> getUnfinishedQuestion(String examId) {
        return subjectiveMapper.queryUnfinishedQuestion(examId);
    }

    @Override
    public List<SingleQuestion> getRandomQuestionByCondition(JSONObject jsonObject) {
        return subjectiveMapper.queryRandomQuestionByCondition(jsonObject);
    }

    @Override
    public Page<SubjectiveQuestion> getAllQuestion(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<SubjectiveQuestion> list = subjectiveMapper.queryAllQuestion();
        return (Page<SubjectiveQuestion>) PageUtils.getPage(list);
    }

    @Override
    public Page<SubjectiveQuestion> getQuestionByCondition(JSONObject condition) {

        Integer currentPage = condition.getInteger("currentPage");
        PageHelper.startPage(currentPage, 10);
        List<SubjectiveQuestion> list = subjectiveMapper.queryQuestionByCondition(condition);
        return (Page<SubjectiveQuestion>) PageUtils.getPage(list);
    }

    @Override
    public Page<SubjectiveQuestion> getFinishedExamAnswer(String examId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, 10);
        List<SubjectiveQuestion> list = subjectiveMapper.queryFinishedExamAnswer(examId);
        return (Page<SubjectiveQuestion>) PageUtils.getPage(list);
    }
}
